package com.briup.cms.web.controller;

import com.briup.cms.bean.Article;
import com.briup.cms.bean.extend.ArticleExtend;
import com.briup.cms.service.IArticleService;
import com.briup.cms.utils.Message;
import com.briup.cms.utils.MessageUtil;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@Api(
        tags = {"咨询信息相关接口"}
)
@RestController
@RequestMapping({"/auth/article"})
public class ArticleController {
    @Resource
    private IArticleService articleService;

    public ArticleController() {
    }

    @ApiOperation(
            value = "分页查询所有的咨询信息",
            notes = "同时查询出对应的栏目信息和用户信息"
    )
    @ApiImplicitParams({@ApiImplicitParam(
            name = "pageNum",
            value = "当前页",
            required = true,
            paramType = "query"
    ), @ApiImplicitParam(
            name = "pageSize",
            value = "每页大小",
            required = true,
            paramType = "query"
    ), @ApiImplicitParam(
            name = "title",
            value = "咨询标题",
            paramType = "query"
    ), @ApiImplicitParam(
            name = "status",
            value = "咨询状态",
            paramType = "query"
    ), @ApiImplicitParam(
            name = "categoryId",
            value = "栏目id",
            paramType = "query"
    )})
    @GetMapping({"/page/queryWithCategoryAndUser"})
    public Message pageQueryWithCategoryAndUser(int pageNum, int pageSize, String title, Integer status, Integer categoryId) {
        PageInfo<ArticleExtend> data = articleService.pageQueryArticleWithCategoryAndUser(pageNum, pageSize, title, status, categoryId);
        return MessageUtil.success(data);
    }

    @ApiOperation(
            value = "通过id查询咨询信息",
            notes = "包括栏目信息、用户信息"
    )
    @ApiImplicitParams({@ApiImplicitParam(
            name = "id",
            value = "主键",
            paramType = "query"
    )})
    @GetMapping({"/findById"})
    public Message findByIdWithCategoryAndUser(int id) {
        ArticleExtend articleExtend = articleService.findByIdWithCategoryAndUser(id);
        return MessageUtil.success(id);
    }

    @ApiOperation("通过id删除")
    @ApiImplicitParams({@ApiImplicitParam(
            name = "id",
            value = "主键",
            paramType = "query",
            required = true
    )})
    @DeleteMapping({"/deleteById"})
    public Message deleteById(int id) {
        this.articleService.deleteById(id);
        return MessageUtil.success("删除成功");
    }

    @ApiOperation(
            value = "保存或更新消息信息",
            notes = "如果参数中包含id后端认为是更新操作，如果参数中不包含id认为是插入操作"
    )
    @PostMapping({"/saveOrUpdate"})
    public Message saveOrUpdate(Article article) {
        this.articleService.saveOrUpdate(article);
        return MessageUtil.success("操作成功");
    }

    @ApiOperation("分页获取所有推荐的咨询信息")
    @ApiImplicitParams({@ApiImplicitParam(
            name = "page",
            value = "当前页",
            required = true,
            paramType = "query"
    ), @ApiImplicitParam(
            name = "pageSize",
            value = "每页大小",
            required = true,
            paramType = "query"
    )})
    @GetMapping({"/page/query/recommend"})
    public Message pageQueryRecommend(int page, int pageSize) {
        PageInfo<ArticleExtend> data = articleService.pageQueryRecommend(page, pageSize);
        return MessageUtil.success(data);
    }

    @ApiOperation("修改文章状态")
    @ApiImplicitParams({@ApiImplicitParam(
            name = "id",
            value = "编号",
            required = true,
            paramType = "query"
    ), @ApiImplicitParam(
            name = "status",
            value = "状态,0-待审核，1-不通过，2-通过，3-推荐",
            required = true,
            paramType = "query"
    )})
    @PutMapping({"/changeStatus"})
    public Message changeStatus(int id, int status) {
        articleService.changeStatus(id, status);
        return MessageUtil.success("审核成功");
    }
}
